package com.kingschan.jeasytag.tag.power;

import javax.servlet.jsp.JspException;

import org.json.JSONArray;
import org.json.JSONObject;

import com.kingschan.jeasytag.container.EasyTagContainer;
import com.kingschan.jeasytag.container.impl.EasyTagContainerImpl;
import com.kingschan.jeasytag.core.annotations.tag.*;
import com.kingschan.jeasytag.tag.RootTag;
import com.kingschan.jeasytag.tag.grid.GridTag;
import com.kingschan.util.lang.StringUtil;
import com.kingschan.util.web.ServletUtil;

/**
 *ExportTag
 *@author kingschan
 *date:2013-4-17
 */

@TagMetaData(CssClass = "", tag = "form"
	,parseClass="com.kingschan.jeasytag.core.parse.diyparse.ExportTagParse"	
)
public class ExportTag extends RootTag implements Cloneable{
	private static final long serialVersionUID = 1L;
	@PropertyField
	@StringField(description="title")
	private String title;
	private String initalparams;
	@PropertyField
	@StringField(description="form of action")
	private String action="";
	@PropertyField
	@StringField(description="in field name")
	private String choosefield;	
	@StringField(description="export file name")
	private String fileName="";
	@StringField(description="don't show columns")
	private String disablecloums="";	
	
	private EasyTagContainer container=EasyTagContainerImpl.getInstance();
	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
	
	
	
	

	@Override
	public int doEndTag() throws JspException {		
		
		try {
			if (this.getParent() instanceof GridTag) {
				GridTag grid = (GridTag) this.getParent();	
				autoModelForGrid(grid);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return super.doEndTag();
	}
	
	
	
	/**
	 * autoModelForGrid
	 * @param grid
	 * @throws Exception
	 */
	public void autoModelForGrid(GridTag grid) throws Exception{
		if (null==this.choosefield) {
			this.choosefield=grid.getCheckboxField();
		}

		if(null==this.fileName){
			this.fileName=null==grid.getTitle()?"download":grid.getTitle();
		}
		if (null!=grid.getQueryParams()) {
			JSONArray jsons = new JSONArray();
			JSONObject json = new JSONObject(grid.getQueryParams());
			json.put("name", "target");
			json.put("value", json.getString("target"));
			jsons.put(json);
			json =new JSONObject();
			json.put("name", "filename");
			json.put("value", StringUtil.getEncriptCode(this.fileName,container.getApplictionKey()));
			jsons.put(json);
			this.initalparams=jsons.toString();
		}
		this.title="";		
		this.action=ServletUtil.getApplictionRootUrl(pageContext).concat("/jeasytag/grid");
		ExportTag export = (ExportTag) this.clone();
		grid.setExport(export);
	}
	
	
	
	
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getInitalparams() {
		return initalparams;
	}
	public void setInitalparams(String initalparams) {
		this.initalparams = initalparams;
	}
	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}
	
	public String getChoosefield() {
		return choosefield;
	}
	public void setChoosefield(String choosefield) {
		this.choosefield = choosefield;
	}
	public String getDisablecloums() {
		return disablecloums;
	}

	public void setDisablecloums(String disablecloums) {
		this.disablecloums = disablecloums;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	
	
}
